<!doctype html>
  <title>IndexedDB test</title>
<script>
'use strict';
let db;

async function setupIndexedDBConnection() {
  db = await new Promise((resolve, reject) => {
    let request = window.indexedDB.open('testdb', 1);
    request.onsuccess = () => resolve(request.result);
    request.onerror = (error) => reject(error);;
    request.onupgradeneeded = () => {
    request.result.createObjectStore('store');
    };
  });
}


function registerPagehideToCloseIndexedDBConnection() {
  addEventListener('pagehide', () => {
    db.close();
  });
}

function registerPagehideToStartTransaction() {
  addEventListener('pagehide', () => {
    let transaction = db.transaction(['store'], 'readwrite');
    let store = transaction.objectStore('store');
    store.put("key", "value");

    // Queue a request to close the connection.
    db.close();
  });
}

function registerPagehideToStartAndCommitTransaction() {
  addEventListener('pagehide', () => {
    let transaction = db.transaction(['store'], 'readwrite');
    let store = transaction.objectStore('store');
    store.put("key", "value");

    // Call commit to run the transaction right away.
    transaction.commit();
    // Close the connection.
    db.close();
  });
}

</script>
